package com.zyl.ag;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.zyl.ag.R;
import com.zyl.ag.ui.ai.AiActivity;
import com.zyl.ag.ui.cal.CalActivity;
import com.zyl.ag.ui.home.HomeActivity;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
 

public class MainActivity extends TabActivity implements OnClickListener {
	public static String TAB_TAG_HOME = "home";
	public static String TAB_TAG_CHANNEL = "channel";
	public static String TAB_TAG_CAL = "cal";
	public static String TAB_TAG_AI = "ai";
	public static String TAB_TAG_ACCOUNT = "account";
	public static String TAB_TAG_SEARCH = "search";
	public static String TAB_TAG_DOWNLOAD = "download";
	public static String TAB_TAB_MORE = "more";
	public static TabHost mTabHost;
	private static final int LOGIN_OUT_DIALOG_ID = 4;

	private boolean bLoginFail = false;
 

	static final int COLOR1 = Color.parseColor("#787878");
	static final int COLOR2 = Color.parseColor("#ffffff");
	ImageView mBut1, mBut2, mBut3, mBut4, mBut5;
	TextView mCateText1, mCateText2, mCateText3, mCateText4, mCateText5;

	Intent mHomeItent, mCalIntent, mSearchIntent, mDownloadIntent,
			mAccountIntent, mMoreIntent, mAllCourseIntent, mAiIntent;

	int mCurTabId = R.id.channel1;

	// Animation
	private Animation left_in, left_out;
	private Animation right_in, right_out;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	 
	 
		prepareAnim();
		prepareIntent();
		setupIntent();
		prepareView();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		/* 当点击Tab选项卡的时候，更改当前Tab标签的背景 */
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				mBut1.setImageResource(R.drawable.icon_1_n);
				mBut2.setImageResource(R.drawable.icon_2_n);
				mBut3.setImageResource(R.drawable.icon_3_n);
				mBut4.setImageResource(R.drawable.icon_4_n);
				mBut5.setImageResource(R.drawable.icon_5_n);
				mCateText1.setTextColor(COLOR1);
				mCateText2.setTextColor(COLOR1);
				mCateText3.setTextColor(COLOR1);
				mCateText4.setTextColor(COLOR1);
				mCateText5.setTextColor(COLOR1);
				View v = mTabHost.getCurrentView();
				if (tabId == TAB_TAG_HOME) {

					mBut1.setImageResource(R.drawable.icon_1_c);
					mCateText1.setTextColor(COLOR2);

				} else if (tabId == TAB_TAG_CAL) {

					mBut2.setImageResource(R.drawable.icon_2_c);
					mCateText2.setTextColor(COLOR2);
				} else if (tabId == TAB_TAG_AI) {

					mBut3.setImageResource(R.drawable.icon_3_c);
					mCateText3.setTextColor(COLOR2);
				} else if (tabId == TAB_TAG_DOWNLOAD) {

					mBut4.setImageResource(R.drawable.icon_4_c);
					mCateText4.setTextColor(COLOR2);
				} else if (tabId == TAB_TAB_MORE) {

					mBut5.setImageResource(R.drawable.icon_5_c);
					mCateText5.setTextColor(COLOR2);
				}
				int checkedId = v.getId();
				final boolean o;
				if (mCurTabId < checkedId)
					o = true;
				else
					o = false;
				if (o)
					mTabHost.getCurrentView().startAnimation(left_out);
				else
					mTabHost.getCurrentView().startAnimation(right_out);

				if (o)
					mTabHost.getCurrentView().startAnimation(left_in);
				else
					mTabHost.getCurrentView().startAnimation(right_in);
				mCurTabId = checkedId;
			}
		});
		SysApplication.getInstance().addActivity(this);
	}

	 

	private boolean isNetworkAvailable() {
		final ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		// final NetworkInfo wifi =
		// manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		final NetworkInfo[] info = manager.getAllNetworkInfo();
		for (int i = 0; i < info.length; ++i) {
			if (info[i].isConnected()) {
				return true;
			}
		}

		return false;
	}

	private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	private void prepareView() {
		mBut1 = (ImageView) findViewById(R.id.imageView1);
		mBut2 = (ImageView) findViewById(R.id.imageView2);
		mBut3 = (ImageView) findViewById(R.id.imageView3);
		mBut4 = (ImageView) findViewById(R.id.imageView4);
		mBut5 = (ImageView) findViewById(R.id.imageView5);
		findViewById(R.id.channel1).setOnClickListener(this);
		findViewById(R.id.channel2).setOnClickListener(this);
		findViewById(R.id.channel3).setOnClickListener(this);
		findViewById(R.id.channel4).setOnClickListener(this);
		findViewById(R.id.channel5).setOnClickListener(this);
		mCateText1 = (TextView) findViewById(R.id.textView1);
		mCateText2 = (TextView) findViewById(R.id.textView2);
		mCateText3 = (TextView) findViewById(R.id.textView3);
		mCateText4 = (TextView) findViewById(R.id.textView4);
		mCateText5 = (TextView) findViewById(R.id.textView5);
	}

	private void prepareIntent() {
		mHomeItent = new Intent(this, HomeActivity.class);
 		mCalIntent = new Intent(this, CalActivity.class);
//		mAccountIntent = new Intent(this, AccountActivity.class);
//		mSearchIntent = new Intent(this, SearchActivity.class);
//		mDownloadIntent = new Intent(this, DownloadActivity.class)
//				.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//		mMoreIntent = new Intent(this, MoreActivity.class);
//		mAllCourseIntent = new Intent(this, AllCourseActivity.class);
		mAiIntent = new Intent(this, AiActivity.class)
				.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			mBut1.performClick();
			ShowExitdialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void setupIntent() {
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.category_home,
				R.drawable.icon_1_n, mHomeItent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_CAL,
				R.string.cal, R.drawable.icon_2_n,
				mCalIntent));
		mTabHost
				.addTab(buildTabSpec(TAB_TAG_AI,
						R.string.category_search, R.drawable.icon_3_n,
						mAiIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_DOWNLOAD,
				R.string.category_download, R.drawable.icon_4_n,
				mDownloadIntent));
		// mTabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNT,
		// R.string.category_account, R.drawable.icon_4_n, mAccountIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAB_MORE, R.string.category_more,
				R.drawable.icon_5_n, mMoreIntent));
//		mTabHost.addTab(buildTabSpec(TAB_TAG_CAL,
//				R.string.category_allcourse, R.drawable.icon_5_n,
//				mAllCourseIntent));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	}

	public static void setCurrentTabByTag(String tab) {
		mTabHost.setCurrentTabByTag(tab);

	}

	@Override
	public void onClick(View v) {
		int checkedId = v.getId();

		switch (checkedId) {
		case R.id.channel1:
			mTabHost.setCurrentTabByTag(TAB_TAG_HOME);

			break;
		case R.id.channel2:
			mTabHost.setCurrentTabByTag(TAB_TAG_CAL);

			break;
		case R.id.channel3:
			mTabHost.setCurrentTabByTag(TAB_TAG_AI);

			break;
		case R.id.channel4:
			mTabHost.setCurrentTabByTag(TAB_TAG_DOWNLOAD);

			break;
		case R.id.channel5:
			mTabHost.setCurrentTabByTag(TAB_TAB_MORE);

			break;
		default:
			break;
		}

	}

 


	protected void ShowExitdialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

		builder.setMessage("确定要退出吗?");

		builder.setTitle("提示");

		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();
						SysApplication.getInstance().exit();
						// MainActivity.this.finish();
					}

				});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();

					}

				});

		builder.create().show();

	}

//	private final BroadcastReceiver mNetworkReceiver = new BroadcastReceiver() {
//		@Override
//		public void onReceive(Context context, Intent intent) {
//			final String action = intent.getAction();
//			if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
//				return;
//			}
//			if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
//				mNetworkAvailability = !intent.getBooleanExtra(
//						ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
//			}
//		}
//	};

	protected void onDestroy() {
		 
		super.onDestroy();

	 
	};

}